package com.xinzhitong.www.service.impl;

import com.xinzhitong.www.exception.BusyException;
import com.xinzhitong.www.exception.companyinfo.MultiBoundCompanyException;
import com.xinzhitong.www.mapper.CompanyMapper;
import com.xinzhitong.www.mapper.ConsignMapper;
import com.xinzhitong.www.mapper.PostMapper;
import com.xinzhitong.www.pojo.Company;
import com.xinzhitong.www.pojo.User;
import com.xinzhitong.www.service.CompanyUserInfoService;
import com.xinzhitong.www.utils.FileHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 企业用户信息服务实现类
 *
 * @author 徐鑫 吴展鹏
 */
@Service
@Transactional
public class CompanyUserInfoServiceImpl implements CompanyUserInfoService {

    /**
     * 公司所有信息的存取路径
     */
    private static final String WHOLEPATH = "/static/company";

    /**
     * 公司图片存取路径
     */
    private static final String PICTURESPATH = "/pictures";

    @Resource
    CompanyMapper companyMapper;

    @Resource
    ConsignMapper consignMapper;

    @Resource
    PostMapper postMapper;

    @Override
    public void editCompanyInfo(Company company, MultipartFile logo, MultipartFile[] pictures) {
        //TODO...
    }

    @Override
    public void createCompanyInfo(Company company, MultipartFile logo, MultipartFile[] pictures) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (companyMapper.selectCompanyByUsername(user.getUsername()) != null)
            throw new MultiBoundCompanyException();
        company.setUsername(user.getUsername());
        //添加基本信息
        if (companyMapper.insertCompany(company) == 0)
            throw new BusyException();
        company = companyMapper.selectCompanyByUsername(user.getUsername());
        if (company == null)
            throw new BusyException();

        //保存logo及图片
        try {
            //生成logo路径
            String logoPath = WHOLEPATH + "/" + company.getId() + "/logo.jpg";
            //保存logo
            FileHelper.saveTo(logoPath, logo);
            //更改logo属性
            company.setLogo(logoPath);
            //存入数据库
            if (companyMapper.updateCompanyOfOneFieldByCondition(
                    "logo",
                    logoPath,
                    "username='" + company.getUsername() + "'"
            ) == 0)
                throw new BusyException();

            //批量存入图片
            if (pictures.length != 0)
                companyMapper.deleteCompanyPicture(company.getId());
            for (int i = 0; i < pictures.length; i++) {
                //生成图片保存路径
                String picturePath = WHOLEPATH + "/" + company.getId() + PICTURESPATH + "/" + i + ".jpg";
                //保存图片
                FileHelper.saveTo(picturePath, pictures[i]);
                //存入数据库
                if (companyMapper.insertCompanyPicture(company.getId(), picturePath) == 0)
                    throw new BusyException();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusyException();
        }
    }

    @Override
    public List<Map<String, Object>> createCompanyInfoAcceleratePicture(Company company, Integer logoNum, Integer picturesNum) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (companyMapper.selectCompanyByUsername(user.getUsername()) != null)
            throw new MultiBoundCompanyException();
        company.setUsername(user.getUsername());
        //添加基本信息
        if (companyMapper.insertCompany(company) == 0)
            throw new BusyException();
        company = companyMapper.selectCompanyByUsername(user.getUsername());
        if (company == null)
            throw new BusyException();

        List<Map<String, Object>> x = new ArrayList<Map<String, Object>>();
        Map map = new HashMap<String, Object>();
        //发送logo及图片的目的address
        //生成logo路径
        String logoPath = WHOLEPATH + "/" + company.getId() + "/logo.jpg";
        //更改logo属性
        company.setLogo(logoPath);
        //存入数据库
        map.put("logoPath", logoPath);
        if (companyMapper.updateCompanyOfOneFieldByCondition(
                "logo",
                logoPath,
                "username='" + company.getUsername() + "'"
        ) == 0)
            throw new BusyException();

        //批量存入图片
        if (picturesNum != 0)
            companyMapper.deleteCompanyPicture(company.getId());
        for (int i = 0; i < picturesNum; i++) {
            //生成图片保存路径
            String picturePath = WHOLEPATH + "/" + company.getId() + PICTURESPATH + "/" + i + ".jpg";
            //存入数据库
            String picturePathNo = "picturePath" + i;
            map.put(picturePathNo, picturePath);
            if (companyMapper.insertCompanyPicture(company.getId(), picturePath) == 0)
                throw new BusyException();
        }
        x.add(map);
        return x;
    }

    @Override
    public Map<String, Integer> getView() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Company company = companyMapper.selectCompanyByUsername(user.getUsername());
        Map<String, Integer> res = new HashMap<>();
        res.put("postCount", postMapper.countPostOfTheCompany(company.getId()));
        res.put("consignCount", consignMapper.countCondignOfTheCompany(company.getId()));
        res.put("peopleCount",
                postMapper.countEnrollUserOfPostOfTheCompany(company.getId())
                        + consignMapper.countHasUserOfConsignOfTheCompany(company.getId()));
        return res;
    }

    @Override
    public Company getInfo() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Company company = companyMapper.selectCompanyByUsername(user.getUsername());
        company.setLogo(FileHelper.fileURL + company.getLogo());
        return company;
    }

}
